package MainPackage;

import java.io.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

public class Options {

    String trunk_server = new String("http://trunkadmin.bestshop-group.com/backend");
    String live_server = new String("http://liveadmin.bestshop-group.com/backend");
    String rc_server = new String("http://rcadmin.bestshop-group.com/backend");
    String trunk_db = new String("localhost");
    String live_db = new String("localhost");
    String rc_db = new String("localhost");

    public void readOptionsFromFile() {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document anotherDocument = builder.build(new File("Options.xml"));

            Element optionsElement = anotherDocument.getRootElement();

            Element trunkserver = optionsElement.getChild("TrunkServer");
            if (trunkserver != null) {
                trunk_server = trunkserver.getText();
            } else {
                System.out.println("Trunk server not found");
            }

            Element trunkdb = optionsElement.getChild("TrunkDB");
            if (trunkdb != null) {
                trunk_db = trunkdb.getText();
            } else {
                System.out.println("Trunk DB not found");
            }

            Element liveserver = optionsElement.getChild("LiveServer");
            if (liveserver != null) {
                live_server = liveserver.getText();
            } else {
                System.out.println("Live server not found");
            }

            Element livedb = optionsElement.getChild("LiveDB");
            if (livedb != null) {
                live_db = livedb.getText();
            } else {
                System.out.println("Live DB not found");
            }

            Element rcserver = optionsElement.getChild("RCServer");
            if (rcserver != null) {
                rc_server = rcserver.getText();
            } else {
                System.out.println("RC server not found");
            }

            Element rcdb = optionsElement.getChild("RCDB");
            if (rcdb != null) {
                rc_db = rcdb.getText();
                
            } else {
                System.out.println("RC DB not found");
            }


        } catch (JDOMException | IOException e) {
            System.out.println(e);

        }

        return;
    }
    
    public void writeOptionsToFile(){
        Element optionsElement = new Element("options");
        Document optionsDocument = new Document(optionsElement);
        optionsElement.addContent(new Element("TrunkServer").addContent(trunk_server));
        optionsElement.addContent(new Element("TrunkDB").addContent(trunk_db));
        optionsElement.addContent(new Element("LiveServer").addContent(live_server));
        optionsElement.addContent(new Element("LiveDB").addContent(live_db));
        optionsElement.addContent(new Element("RCServer").addContent(rc_server));
        optionsElement.addContent(new Element("RCDB").addContent(rc_db));
        try {
            XMLOutputter outputter = new XMLOutputter();
            FileWriter writer = new FileWriter("Options.xml");
            outputter.output(optionsDocument, writer);
            writer.close();
        } 
        catch(java.io.IOException e) {
            System.out.println(e);
        }
    }
}
    